package com.aifast.system.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.system.sys.entity.SysTenant;
import com.aifast.system.sys.service.SysTenantService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import com.aifast.common.base.Controller;
/**
 * 租户 控制层
 *
 * @author fujian
 * @date 2022-10-30
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/tenant")
public class SysTenantController extends Controller {

    private final SysTenantService service;

    @SaCheckPermission("tenant:list")
    @GetMapping("list")
    public LayuiDataTable list() {
        return service.list(getSearchValue(), getInt("state"), get("domain"), getPage());
    }

    @AiIdempotent
    @SaCheckPermission(value = {"tenant:add", "tenant:update"}, mode = SaMode.OR)
    @PostMapping("add")
    public R add(@Valid SysTenant sysTenant) {
        return service.insertOrUpdate(sysTenant) ? OK() : ERROR();
    }

    @AiIdempotent
    @SaCheckPermission("tenant:del")
    @GetMapping("del")
    public R del() {
        return service.del(getInt("id"), get("ids")) ? OK() : ERROR();
    }

    @GetMapping("info")
    public R info() {
        return service.info(getInt("id"));
    }

    @SaCheckPermission("tenant:export")
    @GetMapping("export")
    public void export() {
        service.export(get("name"), getInt("state"), get("domain"), response);
    }

}
